package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.base.EnumBase;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.BatchFlowBillEnum;
import com.xbongbong.saas.enums.business.ContractOutstockEnum;
import com.xbongbong.saas.enums.business.CostAdjustEnum;
import com.xbongbong.saas.enums.business.StockFlowBillEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.saas.script.tempenum.TempContractOutstockEnum;
import com.xbongbong.saas.script.tempenum.TempCostAdjustEnum;
import com.xbongbong.saas.service.CostOptimizeExplainService;
import com.xbongbong.sys.model.CompanyModel;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.lang.reflect.Method;
import java.util.stream.Collectors;

import static com.xbongbong.saas.script.help.InitExplainsHelper.commonFiledInit;

/**
 * @author WuHB
 * @date 2021/12/21 17:23
 */
@Service("costOptimizeExplainService")
public class CostOptimizeExplainServiceImpl implements CostOptimizeExplainService {

    private static final Logger LOG = LoggerFactory.getLogger(CostOptimizeExplainServiceImpl.class);

    @Resource
    private CompanyModel companyModel;

    @Resource
    private PaasFormExplainModel paasFormExplainModel;

    /**
     * 修复库存流水解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @Override
    public String repairStockFlowBillExplain(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 修复库存流水解释开始 -------------------");
        long start = DateTimeUtil.getInt();
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidListTemporary();
        corpidList.add("0");
        // 更新表单解释
        Map<String, Object> queryFormExplainMap = new HashMap<>();
        queryFormExplainMap.put("businessType", XbbRefTypeEnum.STOCK_FLOW_BILL.getCode());
        queryFormExplainMap.put("del", 0);
        queryFormExplainMap.put("saasMark", 1);
        int count = 0;
        int notRepaire = 0;
        List<String> errorCorpidList = new ArrayList<>();
        List<String> updateCorpidList = new ArrayList<>();
        for (String corpid : corpidList) {
            queryFormExplainMap.put("corpid", corpid);
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getNoHandleExplainsByFindEntitys(queryFormExplainMap);
            if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                ++count;
                ++notRepaire;
                LOG.warn("corpid: " + corpid + "---走默认解释无需更新!");
                continue;
            }
            List<PaasFormExplainEntity> newPaasFormExplainList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                // 解释为空的走默认解释
                if (Objects.isNull(paasFormExplainEntity) || Objects.equals(paasFormExplainEntity.getExplains(),"[]")) {
                    ++notRepaire;
                    LOG.warn("corpid: " + corpid + "表单formId:---"+paasFormExplainEntity.getId()+"---走默认解释无需更新!");
                    continue;
                }
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if(Objects.equals(StockFlowBillEnum.TYPE.getAttr(), entity.getAttr())){
                        List<ItemPoJo> items = entity.getItems();
                        XbbRefTypeEnum costAdjust = XbbRefTypeEnum.COST_ADJUST;
                        items.removeIf(item -> Objects.equals(costAdjust.getCode(), item.getValue()));
                        List<Object> costList = items.stream().map(ItemPoJo::getValue).collect(Collectors.toList());
                        if (!costList.contains(costAdjust.getCode())) {
                            items.add(new ItemPoJo(costAdjust.getName(), costAdjust.getCode(), false));
                        }
                    }
                }
                paasFormExplainEntity.setExplains(JSONObject.toJSONString(explainList));
                newPaasFormExplainList.add(paasFormExplainEntity);
            }
            try {
                if ( CollectionsUtil.isNotEmpty(newPaasFormExplainList) ) {
                    paasFormExplainModel.updateBatch(newPaasFormExplainList, corpid);
                    updateCorpidList.add(corpid);
                }
                ++count;
                LOG.warn("corpid: " + corpid + "已更新完毕！");
                LOG.warn("当前已更新" + count + "家公司，当总前进度" + count + "/" + +corpidList.size());
            } catch (Exception e) {
                LOG.error("表单解释更新失败", e);
                LOG.warn(corpid + "更新失败，已更新进度" + count + "/" + corpidList.size());
                errorCorpidList.add(corpid);
            }
        }
        JSONObject object = new JSONObject();
        LOG.warn("更新的公司ID:[{}]", JSONObject.toJSONString(updateCorpidList));
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("操作成功,本次共更新" + corpidList.size() + "家公司，其中:" + count + " 家已更新，" + "走默认解释公司共：" + notRepaire + "家");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        object.put("success", "成功");
        return object.toJSONString();
    }

    /**
     * 修复批次流水解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @Override
    public String repairBatchFlowBillExplain(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 修复批次流水解释开始 -------------------");
        long start = DateTimeUtil.getInt();
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidListTemporary();
        corpidList.add("0");
        // 更新表单解释
        Map<String, Object> queryFormExplainMap = new HashMap<>();
        queryFormExplainMap.put("businessType", XbbRefTypeEnum.BATCH_FLOW_BILL.getCode());
        queryFormExplainMap.put("del", 0);
        queryFormExplainMap.put("saasMark", 1);
        int count = 0;
        int notRepaire = 0;
        List<String> errorCorpidList = new ArrayList<>();
        List<String> updateCorpidList = new ArrayList<>();
        for (String corpid : corpidList) {
            queryFormExplainMap.put("corpid", corpid);
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getNoHandleExplainsByFindEntitys(queryFormExplainMap);
            if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                ++count;
                ++notRepaire;
                LOG.warn("corpid: " + corpid + "---走默认解释无需更新!");
                continue;
            }
            List<PaasFormExplainEntity> newPaasFormExplainList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                // 解释为空的走默认解释
                if (Objects.isNull(paasFormExplainEntity) || Objects.equals(paasFormExplainEntity.getExplains(),"[]")) {
                    ++notRepaire;
                    LOG.warn("corpid: " + corpid + "表单formId:---"+paasFormExplainEntity.getId()+"---走默认解释无需更新!");
                    continue;
                }
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if(Objects.equals(BatchFlowBillEnum.TYPE.getAttr(), entity.getAttr())){
                        List<ItemPoJo> items = entity.getItems();
                        XbbRefTypeEnum costAdjust = XbbRefTypeEnum.COST_ADJUST;
                        items.removeIf(item -> Objects.equals(costAdjust.getCode(), item.getValue()));
                        List<Object> costList = items.stream().map(ItemPoJo::getValue).collect(Collectors.toList());
                        if (!costList.contains(costAdjust.getCode())) {
                            items.add(new ItemPoJo(costAdjust.getName(), costAdjust.getCode(), false));
                        }
                    }
                }
                paasFormExplainEntity.setExplains(JSONObject.toJSONString(explainList));
                newPaasFormExplainList.add(paasFormExplainEntity);
            }
            try {
                if ( CollectionsUtil.isNotEmpty(newPaasFormExplainList) ) {
                    paasFormExplainModel.updateBatch(newPaasFormExplainList, corpid);
                    updateCorpidList.add(corpid);
                }
                ++count;
                LOG.warn("corpid: " + corpid + "已更新完毕！");
                LOG.warn("当前已更新" + count + "家公司，当总前进度" + count + "/" + +corpidList.size());
            } catch (Exception e) {
                LOG.error("表单解释更新失败", e);
                LOG.warn(corpid + "更新失败，已更新进度" + count + "/" + corpidList.size());
                errorCorpidList.add(corpid);
            }
        }
        JSONObject object = new JSONObject();
        LOG.warn("更新的公司ID:[{}]", JSONObject.toJSONString(updateCorpidList));
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("操作成功,本次共更新" + corpidList.size() + "家公司，其中:" + count + " 家已更新，" + "走默认解释公司共：" + notRepaire + "家");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        object.put("success", "成功");
        return object.toJSONString();
    }

    @Override
    public String rollBackStockFlowBillExplain(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 回滚库存流水解释开始 -------------------");
        long start = DateTimeUtil.getInt();
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidListTemporary();
        corpidList.add("0");
        // 更新表单解释
        Map<String, Object> queryFormExplainMap = new HashMap<>();
        queryFormExplainMap.put("businessType", XbbRefTypeEnum.STOCK_FLOW_BILL.getCode());
        queryFormExplainMap.put("del", 0);
        queryFormExplainMap.put("saasMark", 1);
        int count = 0;
        int notRepaire = 0;
        List<String> errorCorpidList = new ArrayList<>();
        for (String corpid : corpidList) {
            queryFormExplainMap.put("corpid", corpid);
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getNoHandleExplainsByFindEntitys(queryFormExplainMap);
            if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                ++count;
                ++notRepaire;
                LOG.warn("corpid: " + corpid + "---走默认解释无需更新!");
                continue;
            }
            List<PaasFormExplainEntity> newPaasFormExplainList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                // 解释为空的走默认解释
                if (Objects.isNull(paasFormExplainEntity) || Objects.equals(paasFormExplainEntity.getExplains(),"[]")) {
                    ++notRepaire;
                    LOG.warn("corpid: " + corpid + "表单formId:---"+paasFormExplainEntity.getId()+"---走默认解释无需更新!");
                    continue;
                }
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if(Objects.equals(StockFlowBillEnum.TYPE.getAttr(), entity.getAttr())){
                        List<ItemPoJo> items = entity.getItems();
                        XbbRefTypeEnum costAdjust = XbbRefTypeEnum.COST_ADJUST;
                        items.removeIf(item -> Objects.equals(costAdjust.getCode(), item.getValue()));
                    }
                }
                paasFormExplainEntity.setExplains(JSONObject.toJSONString(explainList));
                newPaasFormExplainList.add(paasFormExplainEntity);
            }
            try {
                if ( CollectionsUtil.isNotEmpty(newPaasFormExplainList) ) {
                    paasFormExplainModel.updateBatch(newPaasFormExplainList, corpid);
                }
                ++count;
                LOG.warn("corpid: " + corpid + "已回滚完毕！");
                LOG.warn("当前已更新" + count + "家公司，当总前进度" + count + "/" + +corpidList.size());
            } catch (Exception e) {
                LOG.error("表单解释更新失败", e);
                LOG.warn(corpid + "更新失败，已更新进度" + count + "/" + corpidList.size());
                errorCorpidList.add(corpid);
            }
        }
        JSONObject object = new JSONObject();
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("操作成功,本次共更新" + corpidList.size() + "家公司，其中:" + count + " 家已更新，" + "走默认解释公司共：" + notRepaire + "家");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        object.put("success", "成功");
        return object.toJSONString();
    }

    @Override
    public String rollBackBatchFlowBillExplain(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 回滚批次流水解释开始 -------------------");
        long start = DateTimeUtil.getInt();
        // 如果没传公司ID就取所有公司
        List<String> corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidListTemporary();
        corpidList.add("0");
        // 更新表单解释
        Map<String, Object> queryFormExplainMap = new HashMap<>();
        queryFormExplainMap.put("businessType", XbbRefTypeEnum.BATCH_FLOW_BILL.getCode());
        queryFormExplainMap.put("del", 0);
        queryFormExplainMap.put("saasMark", 1);
        int count = 0;
        int notRepaire = 0;
        List<String> errorCorpidList = new ArrayList<>();
        for (String corpid : corpidList) {
            queryFormExplainMap.put("corpid", corpid);
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getNoHandleExplainsByFindEntitys(queryFormExplainMap);
            if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                ++count;
                ++notRepaire;
                LOG.warn("corpid: " + corpid + "---走默认解释无需更新!");
                continue;
            }
            List<PaasFormExplainEntity> newPaasFormExplainList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                // 解释为空的走默认解释
                if (Objects.isNull(paasFormExplainEntity) || Objects.equals(paasFormExplainEntity.getExplains(),"[]")) {
                    ++notRepaire;
                    LOG.warn("corpid: " + corpid + "表单formId:---"+paasFormExplainEntity.getId()+"---走默认解释无需更新!");
                    continue;
                }
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                for (FieldAttrEntity entity : explainList) {
                    if(Objects.equals(BatchFlowBillEnum.TYPE.getAttr(), entity.getAttr())){
                        List<ItemPoJo> items = entity.getItems();
                        XbbRefTypeEnum costAdjust = XbbRefTypeEnum.COST_ADJUST;
                        items.removeIf(item -> Objects.equals(costAdjust.getCode(), item.getValue()));
                    }
                }
                paasFormExplainEntity.setExplains(JSONObject.toJSONString(explainList));
                newPaasFormExplainList.add(paasFormExplainEntity);
            }
            try {
                if ( CollectionsUtil.isNotEmpty(newPaasFormExplainList) ) {
                    paasFormExplainModel.updateBatch(newPaasFormExplainList, corpid);
                }
                ++count;
                LOG.warn("corpid: " + corpid + "已回滚完毕！");
                LOG.warn("当前已更新" + count + "家公司，当总前进度" + count + "/" + +corpidList.size());
            } catch (Exception e) {
                LOG.error("表单解释更新失败", e);
                LOG.warn(corpid + "更新失败，已更新进度" + count + "/" + corpidList.size());
                errorCorpidList.add(corpid);
            }
        }
        JSONObject object = new JSONObject();
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("操作成功,本次共更新" + corpidList.size() + "家公司，其中:" + count + " 家已更新，" + "走默认解释公司共：" + notRepaire + "家");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        object.put("success", "成功");
        return object.toJSONString();
    }

    /**
     * 修复成本调整单仓库解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @Override
    public String repairWarehouseExplain(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 修复成本调整单仓库解释开始 -------------------");
        long start = DateTimeUtil.getInt();
        // 需要更新的字段解释集合
        List<FieldAttrEntity> addFieldAttrEntityList = new ArrayList<>(2);
        List<TempCostAdjustEnum> fieldAttrEntityList = new ArrayList<>(2);
        fieldAttrEntityList.add(TempCostAdjustEnum.WAREHOUSE_ID);
        for (TempCostAdjustEnum tempEnum : fieldAttrEntityList) {
            CostAdjustEnum anEnum = tempEnum.getAnEnum();
            // 包装业务枚举类
            EnumBase<CostAdjustEnum> enumBase = new EnumBase<>(anEnum);
            Class<?> clazz = enumBase.t.getClass();
            // 获取业务枚举类中的属性，以集合的形式返回，同一个枚举的属性值只获取一次
            List<String> enumAttrs = InitExplainsHelper.getBusinessEnumAttrs(clazz);
            // 需要更新的字段解释
            FieldAttrEntity fieldAttrEntity = InitExplainsHelper.setField(enumBase, enumAttrs);
            if (fieldAttrEntity == null) {
                continue;
            }
            String saasAttr = fieldAttrEntity.getSaasAttr();
            String attr = saasAttr.substring(0, 1).toUpperCase() + saasAttr.substring(1);
            String methodName = "init" + attr;
            try {
                commonFiledInit(fieldAttrEntity);
                Method method = TempContractOutstockEnum.class.getMethod(methodName, FieldAttrEntity.class);
                method.invoke(tempEnum, fieldAttrEntity);
            } catch (NoSuchMethodException e) {
                LOG.warn("未找到对应字段的初始化方法");
            } catch (Exception e) {
                LOG.error(e.getMessage());
            }
            fieldAttrEntity.setDefaultName(tempEnum.getAnEnum().getAttrName());
            if (Objects.equals(CostAdjustEnum.WAREHOUSE_ID.getAttr(), fieldAttrEntity.getAttr())) {
                fieldAttrEntity.setLinkedText(CostAdjustEnum.WAREHOUSE_ID_LINKED_TEXT.getAttr());
            }
            addFieldAttrEntityList.add(fieldAttrEntity);
        }
        // 公司ID
        List<String> corpidList;
        // 在这里，这个标志意味着是取临时表的公司ID，还是取公司表的公司ID
        // 1或者其他值 -> 取取临时表的公司ID 不传或者0-> 取公司表的公司ID
        Integer enable = productDTO.getEnable();
        if (Objects.isNull(enable) || Objects.equals(enable,0)) {
            corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidList();
        } else {
            corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidListTemporary();;
        }
        corpidList.add("0");
        // 更新表单解释
        Map<String, Object> queryFormExplainMap = new HashMap<>();
        queryFormExplainMap.put("businessType", XbbRefTypeEnum.COST_ADJUST.getCode());
        queryFormExplainMap.put("del", 0);
        queryFormExplainMap.put("saasMark", 1);
        int count = 0;
        int notRepaire = 0;
        List<String> errorCorpidList = new ArrayList<>();
        List<String> updateCorpidList = new ArrayList<>();
        for (String corpid : corpidList) {
            queryFormExplainMap.put("corpid", corpid);
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getNoHandleExplainsByFindEntitys(queryFormExplainMap);
            if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                ++count;
                ++notRepaire;
                LOG.warn("corpid: " + corpid + "---走默认解释无需更新!");
                continue;
            }
            List<PaasFormExplainEntity> newPaasFormExplainList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                // 解释为空的走默认解释
                if (Objects.isNull(paasFormExplainEntity) || Objects.equals(paasFormExplainEntity.getExplains(),"[]")) {
                    ++notRepaire;
                    LOG.warn("corpid: " + corpid + "表单formId:---"+paasFormExplainEntity.getId()+"---走默认解释无需更新!");
                    continue;
                }
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                explainList.removeIf(entity -> Objects.equals(entity.getAttr(), CostAdjustEnum.WAREHOUSE_ID.getAttr()));
                int product = 0;
                for (int i = 0; i < explainList.size(); i++) {
                    if (Objects.equals(explainList.get(i).getAttr(), CostAdjustEnum.ADJUST_TIME.getAttr())) {
                        product = i + 1;
                    }
                }
                explainList.addAll(product, addFieldAttrEntityList);
                paasFormExplainEntity.setExplains(JSONObject.toJSONString(explainList));
                newPaasFormExplainList.add(paasFormExplainEntity);
            }
            try {
                if ( CollectionsUtil.isNotEmpty(newPaasFormExplainList) ) {
                    paasFormExplainModel.updateBatch(newPaasFormExplainList, corpid);
                    updateCorpidList.add(corpid);
                }
                ++count;
                LOG.warn("corpid: " + corpid + "已更新完毕！");
                LOG.warn("当前已更新" + count + "家公司，当总前进度" + count + "/" + +corpidList.size());
            } catch (Exception e) {
                LOG.error("表单解释更新失败", e);
                LOG.warn(corpid + "更新失败，已更新进度" + count + "/" + corpidList.size());
                errorCorpidList.add(corpid);
            }
        }
        JSONObject object = new JSONObject();
        //LOG.warn("更新的公司ID:[{}]", JSONObject.toJSONString(updateCorpidList));
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("操作成功,本次共更新" + corpidList.size() + "家公司，其中:" + count + " 家已更新，" + "走默认解释公司共：" + notRepaire + "家");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        object.put("success", "成功");
        return object.toJSONString();

    }

    /**
     * 回滚成本调整单仓库解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @Override
    public String rollBackWarehouseExplain(ProductDTO productDTO) throws XbbException {
        LOG.warn("---------------- 回滚成本调整单仓库解释开始 -------------------");
        long start = DateTimeUtil.getInt();
        // 公司ID
        List<String> corpidList;
        // 在这里，这个标志意味着是取临时表的公司ID，还是取公司表的公司ID
        // 1或者其他值 -> 取取临时表的公司ID 不传或者0-> 取公司表的公司ID
        Integer enable = productDTO.getEnable();
        if (Objects.isNull(enable) || Objects.equals(enable,0)) {
            corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidList();
        } else {
            corpidList = CollectionUtils.isNotEmpty(productDTO.getCorpidList()) ? productDTO.getCorpidList() : companyModel.getJxcCorpidListTemporary();;
        }
        corpidList.add("0");
        // 更新表单解释
        Map<String, Object> queryFormExplainMap = new HashMap<>();
        queryFormExplainMap.put("businessType", XbbRefTypeEnum.COST_ADJUST.getCode());
        queryFormExplainMap.put("del", 0);
        queryFormExplainMap.put("saasMark", 1);
        int count = 0;
        int notRepaire = 0;
        List<String> errorCorpidList = new ArrayList<>();
        List<String> updateCorpidList = new ArrayList<>();
        for (String corpid : corpidList) {
            queryFormExplainMap.put("corpid", corpid);
            List<PaasFormExplainEntity> paasFormExplainEntityList = paasFormExplainModel.getNoHandleExplainsByFindEntitys(queryFormExplainMap);
            if (CollectionsUtil.isEmpty(paasFormExplainEntityList)) {
                ++count;
                ++notRepaire;
                LOG.warn("corpid: " + corpid + "---走默认解释无需更新!");
                continue;
            }
            List<PaasFormExplainEntity> newPaasFormExplainList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (PaasFormExplainEntity paasFormExplainEntity : paasFormExplainEntityList) {
                // 解释为空的走默认解释
                if (Objects.isNull(paasFormExplainEntity) || Objects.equals(paasFormExplainEntity.getExplains(),"[]")) {
                    ++notRepaire;
                    LOG.warn("corpid: " + corpid + "表单formId:---"+paasFormExplainEntity.getId()+"---走默认解释无需更新!");
                    continue;
                }
                List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
                explainList.removeIf(entity -> Objects.equals(entity.getAttr(), CostAdjustEnum.WAREHOUSE_ID.getAttr()));
                paasFormExplainEntity.setExplains(JSONObject.toJSONString(explainList));
                newPaasFormExplainList.add(paasFormExplainEntity);
            }
            try {
                if ( CollectionsUtil.isNotEmpty(newPaasFormExplainList) ) {
                    paasFormExplainModel.updateBatch(newPaasFormExplainList, corpid);
                    updateCorpidList.add(corpid);
                }
                ++count;
                LOG.warn("corpid: " + corpid + "已更新完毕！");
                LOG.warn("当前已回滚" + count + "家公司，当总前进度" + count + "/" + +corpidList.size());
            } catch (Exception e) {
                LOG.error("表单解释回滚失败", e);
                LOG.warn(corpid + "回滚失败，已回滚进度" + count + "/" + corpidList.size());
                errorCorpidList.add(corpid);
            }
        }
        JSONObject object = new JSONObject();
        //LOG.warn("回滚的公司ID:[{}]", JSONObject.toJSONString(updateCorpidList));
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        LOG.warn("操作成功,本次共回滚" + corpidList.size() + "家公司，其中:" + count + " 家已回滚，" + "走默认解释公司共：" + notRepaire + "家");
        LOG.error("回滚失败公司ID集合" + errorCorpidList.toString());
        object.put("success", "成功");
        return object.toJSONString();
    }
}
